//
//  ViewController.swift
//  DetailList
//
//  Created by mayuncui on 2020/9/21.
//  Copyright © 2020 Myc. All rights reserved.
//

import UIKit
import HandyJSON
import WebKit

class DetailViewController: UIViewController {

    var tableView = UITableView(frame: .zero, style: .plain)
    
    var dataList: [DataModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadData()
        configTableView()

    }
    
    //获取data数据
    func loadData() {
        guard let path = Bundle.main.path(forResource: "wallet", ofType: "json") else { return }
        let data = NSData(contentsOfFile: path)! as Data
        
        do {
            let dict = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: Any]
            if let da = BaseModel.deserialize(from: dict) {
                if let list = da.data {
                    self.dataList = list
                    
                }
            }
            
            self.tableView.reloadData()
        } catch  {
            print("获取数据失败")
        }
        
    }
    
    //UI布局
    func configTableView() {
        view.flex.define({
            $0.addItem(tableView.with({
                $0.delegate = self
                $0.dataSource = self
                $0.separatorStyle = .none
                $0.register(DetailItemCell.self, forCellReuseIdentifier: "DetailItemCell")
                $0.rowHeight = UITableView.automaticDimension
                $0.backgroundColor = .clear
            })).grow(1).shrink(1)
            
        }).paddingBottom(0).layout()
    }


}

// MARK: --------  UITableViewDataSource, UITableViewDelegate --------
extension DetailViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DetailItemCell", for: indexPath) as! DetailItemCell
        cell.dataModel = dataList?[indexPath.row]
        cell.delegate = self
        return cell
    }
    
    
}

// MARK: -------- 链接 --------
extension DetailViewController: DetailItemCellDelegate {
    func DetailItemCellContentLinkClick(link: String) {
        let vc = UIViewController()
        vc.title = "官网界面"
        let webView = WKWebView()
        webView.load(URLRequest(url: URL(string: link)!))
        vc.view.flex.define({
            $0.addItem(webView).grow(1).shrink(1)
            }).layout()
        
        self.navigationController?.pushViewController(vc, animated: true);
    }
    
    func DetailItemCellImgClick(index: Int, imgArr: [String]) {
        let imgPreviewVc = ImagePreviewViewController.init(images: imgArr, index: index)
        self.navigationController?.pushViewController(imgPreviewVc, animated: true)
    }
    
    
}
